package benny_driving_ser.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import atg.taglib.json.util.JSONArray;
import atg.taglib.json.util.JSONException;
import atg.taglib.json.util.JSONObject;
import benny_driving_ser.dao.impl.PersonInfoDaoImpl;
import benny_driving_ser.entity.DriPerInfo;

public class PersonInfoServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7606167723156103987L;

	/**  
	 * Constructor of the object.
	 */
	public PersonInfoServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String action;
		if (null == (action = request.getParameter("action")))
			return;
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		if ("dri-grzlcx".equals(action.trim())) {
			String driid;
			if (null == (driid = request.getParameter("driid")))
				return;
			DriPerInfo dpi = PersonInfoDaoImpl.getInstance().getDriPerInfo(
					Integer.parseInt(driid.trim()));
			JSONObject j = new JSONObject();
			System.out.println(dpi.toString());
			try {
				j.put("drinum", dpi.getDrinum());
				j.put("photourl", dpi.getPhotourl());
				j.put("name", dpi.getName());
				j.put("tel", dpi.getTel());
				j.put("sex", dpi.getSex());
				j.put("cdri", dpi.getCdri());
				j.put("cid", dpi.getCid());
				j.put("did", dpi.getDid());
				j.put("cadd", dpi.getCadd());
				j.put("ladd", dpi.getLadd());
				j.put("etel", dpi.getEtel());
				j.put("ename", dpi.getEname());
				j.put("bankcard", dpi.getBankcard());
				out.print(j.toString());
			} catch (JSONException e) {
				e.printStackTrace();
				out.print("{'drinum':-1}");
			}
		}
		if ("drilist".equals(action.trim())) {
			String driid;
			if (null == (driid = request.getParameter("driid")))
				return;
			List<Integer> ids = new ArrayList<Integer>();
			JSONArray ja;
			try {
				ja = new JSONArray(driid);
				for (int i = 0; i < ja.length(); i++) {
					ids.add(ja.optInt(i));
				}
				List<DriPerInfo> dpi = PersonInfoDaoImpl.getInstance()
						.getDriList(ids);
				JSONObject j = new JSONObject();
				JSONArray mJa = new JSONArray();
				for (DriPerInfo mDpi : dpi) {
					JSONObject mJ = new JSONObject();
					mJ.put("drinum", mDpi.getDrinum());
					mJ.put("name", mDpi.getName());
					mJ.put("tel", mDpi.getTel());
					mJ.put("drisfzh", mDpi.getCid());
					mJ.put("stars", mDpi.getStarts());
					mJ.put("photourl", mDpi.getPhotourl());
					mJ.put("isect", mDpi.getIsect());
					mJa.add(mJ);
				}
				j.put("drivers", mJa);
				out.print(j.toString());
			} catch (JSONException e) {
				e.printStackTrace();
				out.print("{'drivers':[]}");
			}
		}
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
